// pages/shopExcAddr/shopExcAddr.js
var chooseArea = require('../chooseArea/chooseArea.js')
import S_request from '../../utils/requestService.js';
import CONFIG from '../../config.js';
import * as utils from '../../utils/util.js';
const app = getApp()

var show = false;
var item = {};
Page({

  /**
   * 页面的初始数据
   */
  data: {
    item: {
      show: show
    },
    goodId: 0,
    userName: '',
    mobile: '',
    area: '',
    detailAdr: '',
    goodtype: 0, //（商城进入）0   (夺宝进入)1 自动发放. 2  需要手机号码 3 需要邮寄地址
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let goodId = options.goodId;
    let goodtype = options.type;
    if (goodtype && goodtype != 'undefined' && goodtype != '') {
      this.setData({
        goodtype: goodtype
      })
    }
    this.setData({
      goodId: goodId
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function(e) {
    var that = this;
    //请求数据
    chooseArea.updateAreaData(that, 0, e);
    this.setData({
      area: '请选择所在地区',
      colorStyle: '#999;'
    });
  },
  //点击选择城市按钮显示picker-view
  chooseArea: function(e) {
    chooseArea.animationEvents(this, 0, true, 400);
  },
  //隐藏picker-view
  hiddenFloatView: function(e) {
    chooseArea.animationEvents(this, 200, false, 400);
  },
  //滑动事件
  bindChange: function(e) {
    chooseArea.updateAreaData(this, 1, e);
    item = this.data.item;
    let areaStr = item.provinces[item.value[0]].name + " " + item.citys[item.value[1]].name + " " + item.countys[item.value[2]].name;
    console.log("fhaofia===" + areaStr)
    this.setData({
      area: areaStr,
      colorStyle: '#333;'
    });
  },
  nameInputEvent: function(e) {
    this.setData({
      userName: e.detail.value
    })
  },
  mobileInputEvent: function(e) {
    this.setData({
      mobile: e.detail.value
    })
  },
  addrInputEvent: function(e) {
    this.setData({
      detailAdr: e.detail.value
    })
  },
  //地址提交
  commitAddr: function() {
    let title = '';

    if (this.data.userName == '' && this.data.goodtype != 2) {
      title = '请填写联系人'
      this.showToast(title)
    } else if (this.data.mobile == '')  {
      title = '请填写手机号'
      this.showToast(title)
    } else if (this.data.mobile.length < 11) {
      title = '手机号格式不正确'
      this.showToast(title)
    } else if (this.data.area == '' && this.data.goodtype != 2) {
      title = '请选择所在地区'
      this.showToast(title)
    } else if (this.data.detailAdr == '' && this.data.goodtype != 2) {
      title = '请填写详细地址'
      this.showToast(title)
    } else {
      let address = this.data.area + " " + this.data.detailAdr;
      if (this.data.goodtype == 0) {//商城
        S_request.mine.submitShopInfo(this.data.goodId, this.data.mobile, this.data.userName, address, (data) => {
          let orderId = data.order_id;
          console.log("orderId==" + orderId);
          wx.showToast({
            title: '兑换成功',
            icon: 'none',
            duration: 2000
          })
          setTimeout(function() {
            wx.navigateBack({})
          }, 2000)
        })
      } else { //夺宝
        S_request.duobao.excJP(this.data.goodId, this.data.mobile, this.data.userName, address, (result)=>{
          wx.showModal({
            title: '提示',
            content: '领取成功，请注意查收',
            showCancel: false
          })
        })
      }

    }
  },
  showToast: function(title) {
    wx.showToast({
      title: title,
      icon: 'none',
      duration: 2000
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    wx.showShareMenu({
      withShareTicket: true,
      success: function() {}
    });
    let obj = { 'pagepath': CONFIG.SHARE.path }
    S_request.index.recordShare("-1", obj);
    return {
      title: CONFIG.SHARE.title,
      path: CONFIG.SHARE.path,
      imageUrl: CONFIG.SHARE.imageUrl
    }
  }
})